package aooO.crm.Service.impl;

import aooO.crm.Service.ClueService;
import aooO.crm.common.Utils.DateUtils;
import aooO.crm.common.Utils.UUIDUtils;
import aooO.crm.mapper.*;
import aooO.crm.pojo.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    ClueMapper clueMapper;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    ContactsMapper contactsMapper;

    @Autowired
    ClueRemarkMapper clueRemarkMapper;

    @Autowired
    CustomerRemarkMapper curMapper;

    @Autowired
    ContactsRemarkMapper corMapper;

    @Autowired
    ClueActivityRelationMapper carMapper;

    @Autowired
    ContactsActivityRelationMapper contactsActivityRelationMapper;

    @Autowired
    TranMapper tranMapper;

    @Autowired
    TranRemarkMapper tranRemarkMapper;

    @Autowired
    ClueActivityRelationMapper clueActivityMapper;

    @Autowired
    TranHistoryMapper tranHistoryMapper;

    @Override
    public List<Clue> queryClueByConditionForPage(HashMap<String, Object> map) {
        return clueMapper.queryClueByConditionForPage(map);
    }

    @Override
    public int selectTotalPages(HashMap<String, Object> map) {
        return clueMapper.selectTotalPages(map);
    }

    @Override
    public int CreateClue(Clue clue) {
        return clueMapper.CreateClue(clue);
    }

    @Override
    public Clue queryClueById(String id) {
        return clueMapper.queryClueById(id);
    }

    @Override
    public int deleteClueByIds(String[] ids) {
        //删除线索下的标记
        clueRemarkMapper.deleteClueRemarkByClueIds(ids);
        //删除关联的市场活动
        clueActivityMapper.deleteBoundByClueIds(ids);
        int i = clueMapper.deleteClueByIds(ids);
        return i;
    }

    @Override
    public int updateClue(Clue clue) {
        return clueMapper.updateClue(clue);
    }

    @Override
    public Clue selectDetailClueById(String id) {
        return clueMapper.selectDetailClueById(id);
    }

    /* clueId:clueId,
          money:money,
          name:name,
          expectedDate:expectedDate,
          stage:stage,
          activityId:activityId,
          isCreateTran:isCreateTran*/

    @Override
    public void saveConvert(Map<String, Object> map) {

        String clueId=(String)map.get("clueId");
        User user=(User) map.get("user");
        //查出线索的所有信息
        Clue clue=clueMapper.queryClueById(clueId);
        //需要插入的客户
        Customer customer = fullCustomer(clue,user);
        //需要插入的联系人
        Contacts co=fullContacts(clue,user,customer);

        //查到的标记列表
        List<CustomerRemark> customerRemarkList=new ArrayList<>();
        List<ContactsRemark> contactsRemarkList=new ArrayList<>();
            //插入客户表
            customerMapper.insertCustomer(customer);
            //把与联系人有关的信息转换到联系人表
            contactsMapper.insertContact(co);
            //把线索下所有的备注都插入到客户表里 和 联系人表里一份
        //查出线索
        List<ClueRemark> clueRemarkList = clueRemarkMapper.selectByClueId(clue.getId());
        if (clueRemarkList!=null && clueRemarkList.size()>0){
            for (ClueRemark clueRemark:clueRemarkList){
                CustomerRemark cur = fullCustomerRemark(clueRemark, customer);
                ContactsRemark cor=fullContactsRemark(clueRemark,co);
                customerRemarkList.add(cur);
                contactsRemarkList.add(cor);
            }
            //插入标记
            curMapper.insertCustomerRemarks(customerRemarkList);
            corMapper.insertContactsRemarks(contactsRemarkList);
        }

        //通过线索id查询关联关系关系表中的所有activity id
        List<ClueActivityRelation> list = carMapper.selectByClueId(clue.getId());
        //查到的关系集合
        List<ContactsActivityRelation> conArList=new ArrayList<>();

        if (list!=null && list.size()>0){
            //插入到联系人和市场活动的关联关系表中
            ContactsActivityRelation conAr;
            for (ClueActivityRelation car:list){
               conAr=new ContactsActivityRelation();
               conAr.setId(UUIDUtils.uuid());
               conAr.setActivityId(car.getActivityId());
               conAr.setContactsId(co.getId());
               conArList.add(conAr);
            }
            contactsActivityRelationMapper.insertCARelation(conArList);
        }

        //是否创建交易
        if(map.get("isCreateTran").equals("true")){
            //创建交易
            //加入交易
            Tran tran=new Tran();
            tran.setId(UUIDUtils.uuid());
            tran.setOwner(clue.getOwner());
            tran.setMoney((String)map.get("money"));
            tran.setName((String)map.get("name"));
            tran.setExpectedDate((String)map.get("expectedDate"));
            tran.setCustomerId(customer.getId());
            tran.setStage((String)map.get("stage"));
            tran.setSource(clue.getSource());
            tran.setContactsId(co.getId());
            tran.setActivityId((String) map.get("activityId"));
            tran.setContactSummary(clue.getContactSummary());
            tran.setNextContactTime(clue.getNextContactTime());
            tran.setDescription(clue.getDescription());
            tran.setCreateBy(user.getId());
            tran.setCreateTime(DateUtils.FormatDateTime(new Date()));

            tranMapper.insertTran(tran);
            //备注转到交易里面一份
            List<TranRemark> tranRemarkList=new ArrayList<>();
            if (clueRemarkList!=null && clueRemarkList.size()>0){
                for (ClueRemark clueRemark:clueRemarkList){
                    TranRemark remark = fullTranRemark(clueRemark, tran);
                    tranRemarkList.add(remark);
                }
                //插入标记
                tranRemarkMapper.insertTranRemarks(tranRemarkList);
            }

            TranHistory tranHistory=new TranHistory();
            tranHistory.setId(UUIDUtils.uuid());
            tranHistory.setCreateBy(user.getId());
            tranHistory.setCreateTime(DateUtils.FormatDateTime(new Date()));
            tranHistory.setExpectedDate(tran.getExpectedDate());
            tranHistory.setMoney(tran.getMoney());
            tranHistory.setStage(tran.getStage());
            tranHistory.setTranId(tran.getId());
            //创建较交易记录
            tranHistoryMapper.insertTransactionHistory(tranHistory);
        }


        //删除线索下的标记
        clueRemarkMapper.deleteClueRemarkByClueId(clue.getId());
        //删除关联的市场活动
        clueActivityMapper.deleteBoundByClueId(clue.getId());
        //删除线索
        clueMapper.deleteClueById(clue.getId());
    }

    @Override
    public List<String> queryClueStageOfClueGroupByClueStage() {
        return clueMapper.selectClueStageOfClueGroupByClueStage();
    }

    @Override
    public List<Integer> queryCountOfClueGroupByClueStage() {
        return clueMapper.selectCountOfClueGroupByClueStage();
    }

    public Customer fullCustomer(Clue clue,User user){

        Customer customer=new Customer();
        customer.setId(UUIDUtils.uuid());
        customer.setOwner(clue.getOwner());
        customer.setName(clue.getCompany());
        customer.setWebsite(clue.getWebsite());
        customer.setPhone(clue.getPhone());
        customer.setCreateBy(user.getId());
        customer.setCreateTime(DateUtils.FormatDateTime(new Date()));
        customer.setContactSummary(clue.getContactSummary());
        customer.setNextContactTime(clue.getNextContactTime());
        customer.setDescription(clue.getDescription());
        customer.setAddress(clue.getAddress());

        return customer;
    }

    public Contacts fullContacts(Clue clue,User user,Customer customer){

        Contacts co=new Contacts();
        co.setId(UUIDUtils.uuid());
        co.setOwner(clue.getOwner());
        co.setSource(clue.getSource());
        co.setCustomerId(customer.getId());
        co.setAddress(clue.getAddress());
        co.setFullname(clue.getFullname());
        co.setAppellation(clue.getAppellation());
        co.setEmail(clue.getEmail());
        co.setMphone(clue.getMphone());
        co.setJob(clue.getJob());
        co.setDescription(clue.getDescription());
        co.setContactSummary(clue.getContactSummary());
        co.setNextContactTime(clue.getNextContactTime());
        co.setCreateBy(user.getId());
        co.setCreateTime(DateUtils.FormatDateTime(new Date()));

        return co;
    }

    public CustomerRemark fullCustomerRemark(ClueRemark clueRemark,Customer c){

        CustomerRemark customerRemark=new CustomerRemark();
        customerRemark.setId(UUIDUtils.uuid());
        customerRemark.setNoteContent(clueRemark.getNoteContent());
        customerRemark.setCreateBy(clueRemark.getCreateBy());
        customerRemark.setCreateTime(clueRemark.getCreateTime());
        customerRemark.setEditFlag(clueRemark.getEditFlag());
        customerRemark.setCustomerId(c.getId());
        return customerRemark;
    }

    public ContactsRemark fullContactsRemark(ClueRemark clueRemark,Contacts c){

        ContactsRemark contactsRemark=new ContactsRemark();
        contactsRemark.setId(UUIDUtils.uuid());
        contactsRemark.setNoteContent(clueRemark.getNoteContent());
        contactsRemark.setCreateBy(clueRemark.getCreateBy());
        contactsRemark.setCreateTime(clueRemark.getCreateTime());
        contactsRemark.setEditFlag(clueRemark.getEditFlag());
        contactsRemark.setContactsId(c.getId());
        return contactsRemark;
    }

    public TranRemark fullTranRemark(ClueRemark clueRemark,Tran tran){
        TranRemark remark=new TranRemark();
        remark.setId(UUIDUtils.uuid());
        remark.setNoteContent(clueRemark.getNoteContent());
        remark.setCreateBy(clueRemark.getCreateBy());
        remark.setCreateTime(clueRemark.getCreateTime());
        remark.setEditFlag(clueRemark.getEditFlag());
        remark.setTranId(tran.getId());

        return remark;
    }

}
